#!/usr/bin/env bash

# Author:   Zhang Huangbin (zhb(at)iredmail.org)

#---------------------------------------------------------------------
# This file is part of iRedMail, which is an open source mail server
# solution for Red Hat(R) Enterprise Linux, CentOS, Debian and Ubuntu.
#
# iRedMail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iRedMail is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iRedMail.  If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------

export PROG_NAME='iRedMail'
export PROG_NAME_LOWERCASE='iredmail'
export PROG_VERSION='0.7.3-rc2'

# -----------------
# ---- Generic ----
# -----------------

# Maildir format: mbox, Maildir.
export MAILBOX_FORMAT='Maildir'

# Maildir style: hashed, normal.
export MAILDIR_STYLE='hashed'

# Current storage node.
export STORAGE_NODE='vmail1'

# For distribute deployment.
export MYSQL_SERVER='127.0.0.1'
export MYSQL_PORT='3306'
export IMAP_SERVER='127.0.0.1'
export SMTP_SERVER='127.0.0.1'
export AMAVISD_SERVER='127.0.0.1'

# For managesieve service and software.
export USE_MANAGESIEVE='YES'    # Use managesieve service.
export USE_PYSIEVED='YES'       # Use pysieved to provide managesieve service.

# vmail user. uid and gid should be export after user added. Reference:
# functions/user_vmail.sh
export VMAIL_USER_NAME='vmail'
export VMAIL_GROUP_NAME='vmail'
export VMAIL_ADMIN_USER_NAME="${VMAIL_USER_NAME}admin"

# Default virtual domain admin name without domain name (@example.com).
export DOMAIN_ADMIN_NAME='postmaster'

# Hostname.
export HOSTNAME="$(hostname -f)"

# ----------------------------------
# You do *NOT* need to modify them.
# ----------------------------------
export DATE="$(/bin/date +%Y.%m.%d.%H.%M.%S)"

# For perl and run-time locale setting.
export LC_ALL=C

# Debug mode: YES, NO.
export DEBUG='NO'

# Genrate a random string.
# Usage:
#   str="$(${RANDOM_STRING})"
export RANDOM_STRING='eval </dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 30)'

# Tools.
export CONFIG_VIA_DIALOG="${DIALOG_DIR}/config_via_dialog.sh"
# Note: config file will be sourced in file 'conf/core', function 'check_env()'.
export CONFIG_FILE="${ROOTDIR}/config"
export TIP_FILE="${ROOTDIR}/${PROG_NAME}.tips"
export DOC_FILE="${ROOTDIR}/Documentations"
export STATUS_FILE="${ROOTDIR}/.${PROG_NAME}.installation.status"

# Output flag.
export INFO_FLAG="< INFO >"
export ERROR_FLAG="< ERROR >"
export QUESTION_FLAG="< Question >"
export BACKUP_FLAG="< Backup >"
export DEBUG_FLAG="< DEBUG >"

export CONF_MSG="#
# File generated by ${PROG_NAME} (${DATE}):
#
# Version:  ${PROG_VERSION}
# Project:  http://www.iredmail.org/
#
# Community: http://www.iredmail.org/forum/
#
"

# TERM.
if [ X"${TERM}" == X"" ]; then
    export TERM='xterm'
fi

# Logrotate configuration directory.
export LOGROTATE_DIR='/etc/logrotate.d'

# Kernel name.
export KERNEL_NAME="$(uname -s)"

# command: dialog.
export BIN_DIALOG="dialog"
export PKG_DIALOG="dialog"

# command: bzip2.
export BIN_BZIP2='bzip2'
export PKG_BZIP2='bzip2'

# Shell: /sbin/nologin.
export SHELL_NOLOGIN='/sbin/nologin'

# Check hardware architecture.
arch="$(uname -m)"
case $arch in
    i[3456]86) export ARCH='i386' ;;
    x86_64|amd64) export ARCH='x86_64' ;;
    *)
        echo "Your architecture is not supported yet: ${arch}."
        echo "Both i386 and x86_64 are supported by ${PROG_NAME}."
        exit 255
        ;;
esac

# Check distribution.
#   - DISTRO
#   - DISTRO_VERSION
#   - DISTRO_CODENAME
if [ X"${KERNEL_NAME}" == X"Linux" ]; then
    # Directory of RC scripts.
    export DIR_RC_SCRIPTS='/etc/init.d'

    if [ -f /etc/redhat-release ]; then
        # RHEL/CentOS
        export DISTRO='RHEL'

        # RHEL/CentOS 5.x.
        grep '\ 5' /etc/redhat-release &>/dev/null
        if [ X"$?" == X"0" ]; then
            export DISTRO_VERSION='5'
        fi

        # RHEL/CentOS 6.x.
        grep '\ 6' /etc/redhat-release &>/dev/null
        if [ X"$?" == X"0" ]; then
            export DISTRO_VERSION='6'
        fi

        export PKG_ARCH=".${ARCH}"
        if [ X"${DISTRO_VERSION}" == X"6" -a X"${ARCH}" == X"i386" ]; then
            export PKG_ARCH=''
        fi

    elif [ -f /etc/SuSE-release ]; then
        # SLES/OpenSuSE
        export DISTRO='SUSE'

        # SuSE version number. e.g. 11.3, 11.4.
        export DISTRO_VERSION="$(grep 'VERSION' /etc/SuSE-release | awk '{print $3}')"

        # SuSE code name.
        #   - 11.3:
        #   - 11.4: celadon
        export DISTRO_CODENAME="$(grep 'CODENAME' /etc/SuSE-release | awk '{print $3}' |tr [A-Z] [a-z])"

    elif [ -f /etc/lsb-release ]; then
        # Ubuntu
        export DISTRO='UBUNTU'

        # Code name:
        #   - 8.04: hardy
        #   - 8.10: intrepid
        #   - 9.04: jaunty
        #   - 9.10: karmic
        #   - 10.04: lucid
        #   - 10.10: maverick
        #   - 11.04: natty
        export DISTRO_CODENAME="$( grep 'DISTRIB_CODENAME' /etc/lsb-release | awk -F'=' '{print $2}' )"
        # Mark 10.10 (maverick) as 10.04 (lucid).
        if [ X"${DISTRO_CODENAME}" == X"maverick" ]; then
            export DISTRO_CODENAME='lucid'
        fi

    elif [ -f /etc/debian_version ]; then
        # Debian
        export DISTRO='DEBIAN'

        # Detect release version: 5.x, 6.x.
        # Debian 5.
        grep '^5\.' /etc/debian_version &>/dev/null
        if [ X"$?" == X"0" ]; then
            export DISTRO_VERSION='5'
            export DISTRO_CODENAME='lenny'
        fi

        # Debian 6.
        grep '^6\.' /etc/debian_version &>/dev/null
        if [ X"$?" == X"0" ]; then
            export DISTRO_VERSION='6'
            export DISTRO_CODENAME='squeeze'
        fi

        # Override settings.
        if [ X"${DISTRO_CODENAME}" == X"squeeze" ]; then
            export SHELL_NOLOGIN='/usr/sbin/nologin'
        fi
    else
        # Not support yet.
        echo "Your distrobution is not supported yet."
        exit 255
    fi
elif [ X"${KERNEL_NAME}" == X"FreeBSD" ]; then
    # Directory of RC scripts.
    export DIR_RC_SCRIPTS='/usr/local/etc/rc.d'

    export DISTRO='FREEBSD'
else
    # Not support *BSD and other distrobutions yet.
    echo "Error: Your OS is not supported yet."
    exit 255
fi

# install_pkg: function used to install packages without confirm.
# remove_pkg: function used to remove packages without confirm.
# LIST_ALL_PKGS: command used to list all installed packages.
# LIST_FILES_IN_PKG: command used to list files installed by special package.
# enable_service: function (defined in conf/core) used to enable services.
# disable_service: function (defined in conf/core) used to disable services.
if [ X"${DISTRO}" == X"RHEL" ]; then
    # RHEL/CentOS.
    # System user: root. Note: not all OSes have group 'root'.
    export SYS_ROOT_USER='root'
    export SYS_ROOT_GROUP='root'

    # Package management.
    if [ X"${YUM}" != X"" ]; then
        export YUM="${YUM}"
    else
        export YUM="yum -d 2"
    fi
    export install_pkg='install_pkg_rhel'
    export remove_pkg="remove_pkg_rhel"
    export LIST_ALL_PKGS='rpm -qa'
    export LIST_FILES_IN_PKG='rpm -ql'

    # Service control. Defined in file: conf/core.
    export enable_service='enable_service_rh'
    export disable_service='disable_service_rh'

    # Syslog config file: syslog.
    if [ X"${DISTRO_VERSION}" == X"5" ]; then
        export SYSLOG_CONF='/etc/syslog.conf'
    else
        export SYSLOG_CONF='/etc/rsyslog.conf'
    fi
    export SYSLOG_POSTROTATE_CMD='/bin/kill -HUP $(cat /var/run/syslogd.pid 2> /dev/null) 2> /dev/null || true'

    # Crontab related.
    export CRON_SPOOL_DIR="/var/spool/cron"

    # Directory /etc/sysconfig/ on RHEL/CentOS.
    export ETC_SYSCONFIG_DIR='/etc/sysconfig'

    # Iptables rule file.
    export IPTABLES_CONFIG="${ETC_SYSCONFIG_DIR}/iptables"

    # Directory used to store SSL/TLS key/cert file.
    export SSL_FILE_DIR="/etc/pki/tls"

    # Yum repository related.
    export YUM_REPOS_DIR='/etc/yum.repos.d'
    export LOCAL_REPO_NAME="${PROG_NAME}"
    export LOCAL_REPO_FILE="${YUM_REPOS_DIR}/${LOCAL_REPO_NAME}.repo"

    # Override default value.
    export PKG_DIALOG="dialog${PKG_ARCH}"
    export PKG_BZIP2="bzip2${PKG_ARCH}"

elif [ X"${DISTRO}" == X"SUSE" ]; then
    # OpenSuSE.
    # System user: root. Note: not all OSes have group 'root'.
    export SYS_ROOT_USER='root'
    export SYS_ROOT_GROUP='root'

    # Package management.
    export install_pkg='install_pkg_suse'
    export remove_pkg="remove_pkg_suse"
    export LIST_ALL_PKGS='rpm -qa'
    export LIST_FILES_IN_PKG='rpm -ql'

    # Service control.
    export enable_service='enable_service_rh'
    export disable_service='disable_service_rh'

    # Syslog config file: syslog.
    export SYSLOG_CONF='/etc/rsyslog.conf'
    #export SYSLOG_POSTROTATE_CMD='/bin/kill -HUP $(cat /var/run/syslogd.pid 2> /dev/null) 2> /dev/null || true'
    export SYSLOG_POSTROTATE_CMD=''

    # Crontab related.
    export CRON_SPOOL_DIR="/var/spool/cron/tabs"

    # Directory /etc/sysconfig/ on RHEL/CentOS/SuSE.
    export ETC_SYSCONFIG_DIR='/etc/sysconfig'

    # Iptables rule file.
    export IPTABLES_CONFIG="${ETC_SYSCONFIG_DIR}/SuSEfirewall2"

    # Directory used to store SSL/TLS key/cert file.
    export SSL_FILE_DIR="/etc/ssl"

    # Yum repository related.
    export ZYPPER_REPOS_DIR='/etc/zypp/repos.d'
    export LOCAL_REPO_NAME="${PROG_NAME}"
    export LOCAL_REPO_FILE="${YUM_REPOS_DIR}/${LOCAL_REPO_NAME}.repo"

    # Override default value.
    export PKG_DIALOG="dialog"
    export PKG_BZIP2="bzip2"

elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
    # Ubuntu & Debian.
    export DEBIAN_FRONTEND='noninteractive'

    # System user: root. Note: not all OSes have group 'root'.
    export SYS_ROOT_USER='root'
    export SYS_ROOT_GROUP='root'

    # Package management.
    if [ X"${APTGET}" != X"" ]; then
        export APTGET="${APTGET}"
    else
        export APTGET="apt-get"
    fi
    # Package management.
    export install_pkg='install_pkg_debian'
    export remove_pkg='removel_pkg_debian'
    export LIST_ALL_PKGS="dpkg -l |awk '{print $2}'"
    export LIST_FILES_IN_PKG='dpkg -L'

    # Service control.
    export enable_service="enable_service_debian"
    export disable_service="disable_service_debian"

    # Syslog config: rsyslog.
    if [ X"${DISTRO}" == X"UBUNTU" ]; then
        if [ X"${DISTRO_CODENAME}" == X"hardy" \
            -o X"${DISTRO_CODENAME}" == X"intrepid" \
            -o X"${DISTRO_CODENAME}" == X"jaunty" ]; then
            export SYSLOG_CONF='/etc/syslog.conf'
            export SYSLOG_POSTROTATE_CMD='invoke-rc.d sysklogd reload > /dev/null'
        else
            # Ubuntu >= 9.10
            export SYSLOG_CONF='/etc/rsyslog.d/iredmail.conf'
            export SYSLOG_POSTROTATE_CMD='reload rsyslog >/dev/null 2>&1 || true'
        fi
    fi

    if [ X"${DISTRO}" == X"DEBIAN" ]; then
        export SYSLOG_CONF='/etc/rsyslog.conf'
        export SYSLOG_POSTROTATE_CMD='invoke-rc.d rsyslog reload > /dev/null'
    fi

    # Crontab related.
    export CRON_SPOOL_DIR="/var/spool/cron/crontabs"

    # Directory /etc/default/ on Debian/Ubuntu.
    export ETC_SYSCONFIG_DIR='/etc/default'

    # Iptables rule file.
    export IPTABLES_CONFIG="${ETC_SYSCONFIG_DIR}/iptables"

    # Directory used to store SSL/TLS key/cert file.
    export SSL_FILE_DIR="/etc/ssl"

elif [ X"${DISTRO}" == X"FREEBSD" ]; then
    # System user: root.
    export SYS_ROOT_USER='root'
    export SYS_ROOT_GROUP='wheel'

    # Package management.
    if [ X"${PKGADD}" != X"" ]; then
        export PKGADD="${PKGADD}"
    else
        export PKGADD="pkg_add"
    fi

    # Package management.
    export install_pkg='install_pkg_freebsd'
    export remove_pkg='removel_pkg_freebsd'
    export LIST_ALL_PKGS="pkg_info | awk '{print $1}'"
    export LIST_FILES_IN_PKG='pkg_info -L'

    # Service control.
    export enable_service="enable_service_freebsd"
    export disable_service="disable_service_freebsd"

    # Syslog config: rsyslog.
    export SYSLOG_CONF='/etc/syslog.conf'
    export SYSLOG_POSTROTATE_CMD=''

    # Crontab related.
    export CRON_SPOOL_DIR="/var/cron/tabs"

    # Directory /etc/defaults/ on Debian/Ubuntu.
    export ETC_SYSCONFIG_DIR='/etc/defaults'

    # IPFW rule file.
    export IPTABLES_CONFIG="${ETC_SYSCONFIG_DIR}/ipfw.rules"

    # Directory used to store SSL/TLS key/cert file.
    export SSL_FILE_DIR="/etc/ssl"

else
    # Not support yet.
    echo "Your distrobution is not supported yet."
    exit 255
fi

# Use a seperated directory to store iRedMail certs/keys.
export SSL_CERT_DIR="${SSL_FILE_DIR}/certs"
export SSL_KEY_DIR="${SSL_FILE_DIR}/private"

# SSL key.
export OPENSSL_CONF="${SSL_FILE_DIR}/openssl.cnf"
export SSL_CERT_FILE="${SSL_CERT_DIR}/iRedMail_CA.pem"
export SSL_KEY_FILE="${SSL_KEY_DIR}/iRedMail.key"
export TLS_COUNTRY='CN'
export TLS_STATE='GuangDong'
export TLS_CITY='ShenZhen'
export TLS_COMPANY="${HOSTNAME}"
export TLS_DEPARTMENT='IT'
export TLS_HOSTNAME="${HOSTNAME}"
export TLS_ADMIN="root@${HOSTNAME}"

# Default SSL key size in bits
export SSL_KEY_SIZE='2048'
default_ssl_key_size="$(grep '^default_bits' ${OPENSSL_CONF} 2>/dev/null | awk -F'=' '{print $2}' | awk '{print $1}')"
if [ -z ${default_ssl_key_size} ]; then
    :
elif [ ${default_ssl_key_size} -ge ${SSL_KEY_SIZE} ]; then
    export SSL_KEY_SIZE="${default_ssl_key_size}"
fi

# Location of sshd_config
export SSHD_CONFIG='/etc/ssh/sshd_config'

# MySQL databases which need to be backed up.
export BACKUP_DATABASES=''
